package com.dengqi.mymodule.activity.popupwindow;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RadioButton;

import com.dengqi.mymodule.BaseActivity;
import com.dengqi.mymodule.R;

public class PopActivity extends BaseActivity implements View.OnClickListener{

    // horizontal radio buttons
    private RadioButton alignLeftRb;
    private RadioButton alignRightRb;
    private RadioButton centerHoriRb;
    private RadioButton toRightRb;
    private RadioButton toLeftRb;
    // vertical radio buttons
    private RadioButton alignAboveRb;
    private RadioButton alignBottomRb;
    private RadioButton centerVertRb;
    private RadioButton toBottomRb;
    private RadioButton toAboveRb;

    private CustomPopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop);

        alignLeftRb= findViewById(R.id.align_left_rb);
        alignRightRb= findViewById(R.id.align_right_rb);
        centerHoriRb= findViewById(R.id.center_hori_rb);
        toRightRb= findViewById(R.id.to_right_rb);
        toLeftRb= findViewById(R.id.to_left_rb);

        alignAboveRb= findViewById(R.id.align_above_rb);
        alignBottomRb= findViewById(R.id.align_bottom_rb);
        centerVertRb= findViewById(R.id.center_vert_rb);
        toBottomRb= findViewById(R.id.to_bottom_rb);
        toAboveRb= findViewById(R.id.to_above_rb);

        alignLeftRb.setOnClickListener(this);
        alignRightRb.setOnClickListener(this);
        centerHoriRb.setOnClickListener(this);
        toRightRb.setOnClickListener(this);
        toLeftRb.setOnClickListener(this);

        alignAboveRb.setOnClickListener(this);
        alignBottomRb.setOnClickListener(this);
        centerVertRb.setOnClickListener(this);
        toBottomRb.setOnClickListener(this);
        toAboveRb.setOnClickListener(this);

        popupWindow = new CustomPopupWindow(this);
    }

    public void onBtn1(View view){
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_message,null);
        PopupWindow window=new PopupWindow(contentView, 500, 500, true);
        // 设置PopupWindow的背景
        window.setBackgroundDrawable(new ColorDrawable(Color.BLACK));
        // 设置PopupWindow是否能响应外部点击事件
        window.setOutsideTouchable(true);
        // 设置PopupWindow是否能响应点击事件
        window.setTouchable(true);
        // 显示PopupWindow，其中：
        // 第一个参数是PopupWindow的锚点，第二和第三个参数分别是PopupWindow相对锚点的x、y偏移
        window.showAsDropDown(view, 0, 0);
        // 或者也可以调用此方法显示PopupWindow，其中：
        // 第一个参数是PopupWindow的父View，第二个参数是PopupWindow相对父View的位置，
        // 第三和第四个参数分别是PopupWindow相对父View的x、y偏移
        // 相对于屏幕
//         window.showAtLocation(view, Gravity.CENTER, 0, 0);

    }

    public void onBtn2(View view){

        popupWindow.show(view);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.align_left_rb:
                popupWindow.setHoriGravity(CommonPopupWindow.LayoutGravity.ALIGN_LEFT);
                break;
            case R.id.align_right_rb:
                popupWindow.setHoriGravity(CommonPopupWindow.LayoutGravity.ALIGN_RIGHT);
                break;
            case R.id.center_hori_rb:
                popupWindow.setHoriGravity(CommonPopupWindow.LayoutGravity.CENTER_HORI);
                break;
            case R.id.to_right_rb:
                popupWindow.setHoriGravity(CommonPopupWindow.LayoutGravity.TO_RIGHT);
                break;
            case R.id.to_left_rb:
                popupWindow.setHoriGravity(CommonPopupWindow.LayoutGravity.TO_LEFT);
                break;
            case R.id.align_above_rb:
                popupWindow.setVertGravity(CommonPopupWindow.LayoutGravity.ALIGN_ABOVE);
                break;
            case R.id.align_bottom_rb:
                popupWindow.setVertGravity(CommonPopupWindow.LayoutGravity.ALIGN_BOTTOM);
                break;
            case R.id.center_vert_rb:
                popupWindow.setVertGravity(CommonPopupWindow.LayoutGravity.CENTER_VERT);
                break;
            case R.id.to_bottom_rb:
                popupWindow.setVertGravity(CommonPopupWindow.LayoutGravity.TO_BOTTOM);
                break;
            case R.id.to_above_rb:
                popupWindow.setVertGravity(CommonPopupWindow.LayoutGravity.TO_ABOVE);
                break;
        }
    }
}
